package dev.android.yeqh.tvannounce.common;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.LinkRegexFilter;
import org.htmlparser.filters.RegexFilter;
import org.htmlparser.util.NodeList;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import android.content.Context;
import android.widget.Toast;
import dev.android.yeqh.tvannounce.model.Area;
import dev.android.yeqh.tvannounce.model.Channel;
import dev.android.yeqh.tvannounce.model.Program;
import dev.android.yeqh.tvannounce.net.AccessWS;

public class Utils {
	public static Document getDocument(String result) throws SAXException, IOException, ParserConfigurationException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		InputStream is = new ByteArrayInputStream(result.getBytes("UTF-8"));
		return builder.parse(is);
	}

	public static List<Area> getAreas() throws ClientProtocolException, ParserConfigurationException, IOException, SAXException {
		if (Globes.areas == null || Globes.areas.size() == 0) {
			Globes.areas = new ArrayList<Area>();
	    	
	    	Area area = null;
	    	
	    	for (String areaName : AreaInfo.areas) {
		    	area = new Area();
		    	area.setAreaName(areaName);
		    	
		  		Globes.areas.add(area);
	    	}
		}
		
		return Globes.areas;
	}
	
	public static List<Channel> getChannels(String areaName) throws Exception {
		List<Channel> channels = new ArrayList<Channel>();
		Channel channel = null;
		
		Map<String, String> stations = AreaInfo.stationMap.get(areaName);
		
        for (String key : stations.keySet()) {
               channel = new Channel();
               channel.setChannelId(key);
               channel.setChannelName(stations.get(key));
               
               channels.add(channel);
        }
		
		return channels;
	}
	
	public static List<Channel> getSubChannels(String stationId) throws Exception {
		List<Channel> channels = new ArrayList<Channel>();
		Channel channel = null;
		
		String url = "http://wap.tvmao.com/abc_tvstation.jsp?p=" + stationId;
    	String result = AccessWS.getPage(url);
    	
    	NodeFilter filter = new LinkRegexFilter("^/program\\.jsp\\?c=");
    	NodeList nodes = new Parser(result).extractAllNodesThatMatch(filter);
    	for (int i = 0; i < nodes.size(); i++) {
    		Node node = nodes.elementAt(i);
    		
    		channel = new Channel();
			String value = node.getText();
			channel.setChannelId(value.split("\"")[1].substring(15));
			channel.setChannelName(node.getFirstChild().getText());
			
			channels.add(channel);
		}
		
		return channels;
	}
	
	public static List<Channel> getSaTVs() throws Exception {
		List<Channel> channels = new ArrayList<Channel>();
		Channel channel = null;
		
		String url = "http://wap.tvmao.com/ws_list.jsp";
    	String result = AccessWS.getPage(url);
    	
    	NodeFilter filter = new LinkRegexFilter("^/program\\.jsp\\?c=");
    	NodeList nodes = new Parser(result).extractAllNodesThatMatch(filter);
    	for (int i = 0; i < nodes.size(); i++) {
    		Node node = nodes.elementAt(i);
    		
    		channel = new Channel();
			String value = node.getText();
			channel.setChannelId(value.split("\"")[1].substring(15));
			channel.setChannelName(node.getFirstChild().getText());
			
			channels.add(channel);
		}
    	
    	url = "http://wap.tvmao.com/ws_list.jsp?start=1&fr=";
    	result = AccessWS.getPage(url);
    	
    	nodes = new Parser(result).extractAllNodesThatMatch(filter);
    	for (int i = 0; i < nodes.size(); i++) {
    		Node node = nodes.elementAt(i);
    		
    		channel = new Channel();
			String value = node.getText();
			channel.setChannelId(value.split("\"")[1].substring(15));
			channel.setChannelName(node.getFirstChild().getText());
			
			channels.add(channel);
		}
		
		return channels;
	}
	
	public static List<Program> getPrograms(String channelId) throws Exception {
		List<Program> programs = new ArrayList<Program>();
		Program program = null;
		
		String url = "http://wap.tvmao.com/program.jsp?c=" + channelId;
    	String result = AccessWS.getPage(url);
    	
    	NodeFilter filter = new RegexFilter("^[0-9]{2}:[0-9]{2} .*$");
    	NodeList nodes = new Parser(result).extractAllNodesThatMatch(filter);
    	for (int i = 0; i < nodes.size(); i++) {
    		program = new Program();
			String value = nodes.elementAt(i).getText();
			program.setProgramTime(value.split(" ")[0]);
			program.setProgramName(value.split(" ")[1]);
			
			programs.add(program);
		}
		
		return programs;
	}
	
	public static void showMessage(Context context, Exception e) {
		Toast.makeText(context, e.toString(), 1000).show();
		e.printStackTrace();
	}
}
